package com.xh.bussiness.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.StudentCallLogRequest;
import com.xh.bussiness.system.model.StudentCall;
import com.xh.bussiness.system.model.StudentCallLog;
import com.xh.bussiness.system.service.IStudentCallLogService;
import com.xh.bussiness.system.service.IStudentCallService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生回访通讯录 前端控制器
 * </p>
 *
 * @author JQM
 * @since 2019-12-03
 */
@Api(tags = "学生回访相关")
@RestController
@RequestMapping("/admin/studentCall")
public class StudentCallController extends BaseController {

    @Autowired
    private IStudentCallService studentCallService;
    @Autowired
    private IStudentCallLogService studentCallLogService;
    /**
     * 新增和保存
     * @param studentCall 实体对象
     * @return
     */
    @ApiOperation("新增和保存")
    @RequestMapping(value="/saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public AjaxResult saveOrUpdate(StudentCall studentCall) {
        if (StringUtils.isEmpty(studentCall.getId())) {
            studentCallService.insert(studentCall);
        } else {
            studentCallService.update(studentCall);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, studentCall);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation("删除操作")
    @RequestMapping("/deleteById")
    @ResponseBody
    @Transactional
    public AjaxResult deleteById(String id) {
        studentCallService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    /**
     * 根据学生查询回访通讯录
     * @param studentId
     * @return
     */
    @ApiOperation("根据学生查询回访通讯录")
    @ApiImplicitParam(name = "studentId", value = "学生id")
    @RequestMapping("/queryByStudentId")
    @ResponseBody
    public AjaxResult queryByStudentId(String studentId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("student_id", studentId);
        queryWrapper.orderByAsc("create_time");
        List list = studentCallService.selectList(queryWrapper, StudentCall.class);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, list);
    }

    /**
     * 老师回访
     *
     * @param request
     * @return
     */
    @ApiOperation("老师回访")
    @RequestMapping("/returnCall")
    @ResponseBody
    @Transactional
    public AjaxResult returnCall(@Valid StudentCallLogRequest request, BindingResult result) {
        if (result.hasErrors()) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, result.getFieldError().getDefaultMessage());
        }
        studentCallLogService.insertObj(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, request);
    }

    @ApiOperation("教师查询回访记录")
    @ApiImplicitParam(name = "teacherId",value = "教师id")
    @RequestMapping("/queryStudentCallLogByTeacherId")
    public AjaxResult queryStudentCallLogByTeacherId(String teacherId) {
        if (StringUtils.isEmpty(teacherId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id", teacherId);
        queryWrapper.orderByDesc("create_time");

        List<StudentCallLog> list = studentCallLogService.selectList(queryWrapper, StudentCallLog.class);
        if (CollectionUtils.isEmpty(list)) {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, new ArrayList<>());
        }
        Map<String, List<StudentCallLog>> result = list.stream().collect(Collectors.groupingBy(item -> item.getStudentName()));
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, result);
    }
}

